package com.tjp.subject.domain.service.impl;

import com.tjp.subject.common.enums.IsDeletedFlagEnum;
import com.tjp.subject.domain.convert.SubjectLabelConverter;
import com.tjp.subject.domain.entity.SubjectLabelBO;
import com.tjp.subject.domain.service.SubjectLabelDomainService;
import com.tjp.subject.infra.basic.entity.SubjectLabel;
import com.tjp.subject.infra.basic.entity.SubjectMapping;
import com.tjp.subject.infra.basic.service.SubjectLabelService;
import com.tjp.subject.infra.basic.service.SubjectMappingService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: jz-club
 * @description: 标签领域层接口实现类
 * @author: aaronTang
 * @create: 2024-02-27
 */
@Service
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    public void insert(SubjectLabelBO subjectLabelBO) {

        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBOToLabelEntity(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UNDELETED.getCode());

        SubjectLabel insert = subjectLabelService.insert(subjectLabel);

    }

    public int update(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBOToLabelEntity(subjectLabelBO);

        int update = subjectLabelService.update(subjectLabel);
        return update;
    }

    public int delete(SubjectLabelBO subjectLabelBO) {
        subjectLabelBO.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBOToLabelEntity(subjectLabelBO);

        int update = subjectLabelService.update(subjectLabel);
        return update;
    }

    public List<SubjectLabelBO> queryLabel(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBOToLabelEntity(subjectLabelBO);

        List<SubjectLabel> subjectLabels = subjectLabelService.queryListByCategoryId(subjectLabel);
        List<SubjectLabelBO> subjectLabelBOS = SubjectLabelConverter.INSTANCE.convertEntityListToBOList(subjectLabels);

        return subjectLabelBOS;
    }

    public List<SubjectLabelBO> quertyLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(subjectLabelBO.getCategoryId());
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UNDELETED.getCode());

        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
        if (CollectionUtils.isEmpty(mappingList)) {
            return Collections.emptyList();
        }

        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());

        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
        List<SubjectLabelBO> boList = new ArrayList<>();
        //组装bo
        labelList.forEach(label -> {
            SubjectLabelBO bo = new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            bo.setCategoryId(subjectLabelBO.getCategoryId());
            boList.add(bo);
        });
        return boList;
    }
}
